#include "StdAfx.h"
#include "DirectionalLight.h"


CDirectionalLight::CDirectionalLight(void)
{
}


CDirectionalLight::~CDirectionalLight(void)
{
}

CColor4 CDirectionalLight::Shade(const CRay& vRay, const CVector3D& vPoint, const CVector3D& vNormal, const CGeometry& geometry, DATA_TYPE fTransparency) const
{
	CColor4 ambient = geometry.GetColor() * m_vColor * m_fAmbient;
	if (fTransparency < EPSILON)
	{
		return ambient;
	}

	CVector3D l = -m_vDirection;

	CVector3D vTemp = (l - vRay.GetDir());
	CVector3D h = vTemp / vTemp.GetLength();

	CColor4 diffuse = geometry.GetColor() * m_vColor * __max(0, vNormal.Dot(l));
	CColor4 specular = m_vColor * __max(0, powf(vNormal.Dot(h), geometry.GetMaterial().phongExponent));

	return ambient + fTransparency * (diffuse + specular);
}

void CDirectionalLight::Init(const CVector3D& vPosition, const CVector3D& vDirection, DATA_TYPE fAmbient, const CColor4& vColor)
{
	__super::Init(vPosition, fAmbient, vColor);
	m_vDirection = vDirection;
}

CRay CDirectionalLight::GetRayToLightAndDistance(const CVector3D& vPoint, DATA_TYPE& fDistance) const
{
	fDistance = std::numeric_limits<DATA_TYPE>::max();
	return CRay(vPoint, -m_vDirection);
}